﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Web.Configuration;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Security.Principal;
using System.Collections.Specialized;
using System.Diagnostics;

namespace doris.Agent {
    public partial class Settings : System.Web.UI.Page {
/*        DorisMembershipProvider Membership;
            Membership = new DorisMembershipProvider();
            NameValueCollection config = new NameValueCollection();
            config.Add("name", "DorisMembershipProvider");
            config.Add("type", "DorisMembershipProvider");
            config.Add("applicationName", "/");
            config.Add("connectionStringName", "localConnectionString");

            Membership.Initialize("DorisMembershipProvider", config);
*/
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                if (getConfigurationItem("NewQuestions") == "true")
                    Configs.Items[0].Selected = true;
                else
                    Configs.Items[0].Selected = false;

                if (getConfigurationItem("ReplyAll") == "true")
                    Configs.Items[1].Selected = true;
                else
                    Configs.Items[1].Selected = false;

                if (getConfigurationItem("ReplyMine") == "true")
                    Configs.Items[2].Selected = true;
                else
                    Configs.Items[2].Selected = false;
            }
        }

        public void storeConfigurationItem(String Type, String Value) {
            MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
            try {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;

                IPrincipal currentUser = HttpContext.Current.User; 
//                bool writeEnable = currentUser.IsInRole("Administrator") 
//                bool blabla = currentUser.IsInRole("Operator");

//    IN in_configuration_type varchar(45),
//    IN in_configuration_value varchar(255),
//    IN in_userid varchar(36))
                //          Console.WriteLine("Connecting to MySQL...");
//                cmd.CommandText = "getTicketStatus";
                cmd.CommandText = "storeConfiguration";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new MySqlParameter("@in_configuration_type", MySqlDbType.VarChar));
                cmd.Parameters["@in_configuration_type"].Value = Type;
                cmd.Parameters["@in_configuration_type"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add(new MySqlParameter("@in_configuration_value", MySqlDbType.VarChar));
                cmd.Parameters["@in_configuration_value"].Value = Value;
                cmd.Parameters["@in_configuration_value"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add(new MySqlParameter("@in_username", MySqlDbType.VarChar));
                cmd.Parameters["@in_username"].Value = currentUser.Identity.Name;
                cmd.Parameters["@in_username"].Direction = ParameterDirection.Input;
                
                cmd.ExecuteNonQuery();
                
                cmd.Dispose();
            } catch (MySql.Data.MySqlClient.MySqlException ex) {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            } finally {
                conn.Close();
            }
        }

        public String getConfigurationItem(String Type) {
            MySqlConnection conn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["localConnectionString"].ConnectionString);
            String Value = null;
            try {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;

                IPrincipal currentUser = HttpContext.Current.User;
                //                bool writeEnable = currentUser.IsInRole("Administrator") 
                //                bool blabla = currentUser.IsInRole("Operator");

                //    IN in_configuration_type varchar(45),
                //    IN in_configuration_value varchar(255),
                //    IN in_userid varchar(36))
                //          Console.WriteLine("Connecting to MySQL...");
                //                cmd.CommandText = "getTicketStatus";
                cmd.CommandText = "getConfiguration";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new MySqlParameter("@in_configuration_type", MySqlDbType.VarChar));
                cmd.Parameters["@in_configuration_type"].Value = Type;
                cmd.Parameters["@in_configuration_type"].Direction = ParameterDirection.Input;

                cmd.Parameters.Add(new MySqlParameter("@out_configuration_value", MySqlDbType.VarChar));
                cmd.Parameters["@out_configuration_value"].Direction = ParameterDirection.Output;

                cmd.Parameters.Add(new MySqlParameter("@in_username", MySqlDbType.VarChar));
                cmd.Parameters["@in_username"].Value = currentUser.Identity.Name;
                cmd.Parameters["@in_username"].Direction = ParameterDirection.Input;

                cmd.ExecuteNonQuery();

                Value = cmd.Parameters["@out_configuration_value"].Value.ToString();

                cmd.Dispose();
            } catch (MySql.Data.MySqlClient.MySqlException ex) {
                Console.WriteLine("Error " + ex.Number + " has occurred: " + ex.Message);
            } finally {
                conn.Close();
            }
            return Value;
        }

        protected void Button1_Click(object sender, EventArgs e) {
            if (Configs.Items[0].Selected) {
                Debug.WriteLine("nq: true");
                storeConfigurationItem("NewQuestions", "true"); // nye spm
            } else {
                Debug.WriteLine("nq: false");
                storeConfigurationItem("NewQuestions", null); // nye spm
            }
            if (Configs.Items[1].Selected) {
                Debug.WriteLine("ra: true");
                storeConfigurationItem("ReplyAll", "true"); // oppfølginger alle
            } else {
                Debug.WriteLine("ra: false");
                storeConfigurationItem("ReplyAll", null); // oppfølginger alle
            }
            if (Configs.Items[2].Selected) {
                Debug.WriteLine("rm: true");
                storeConfigurationItem("ReplyMine", "true"); // oppfølginger til mine
            } else {
                Debug.WriteLine("rm: false");
                storeConfigurationItem("ReplyMine", null); // oppfølginger til mine
            }
        }
    }
}